﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
namespace jiaju.Models
{
        public class DBHelp
        {
            static string url = "server=.;uid=sa;pwd=123456;database=jiaju";
            public static SqlDataReader ExecuteReader(string sql)
            {
                SqlConnection conn = new SqlConnection(url);
                try
                {
                    conn.Open();
                    SqlCommand comm = new SqlCommand();
                    comm.Connection = conn;
                    comm.CommandText = sql;
                    SqlDataReader sdr = comm.ExecuteReader();
                    return sdr;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    conn.Close();
                    return null;
                }
            }
            //2:查询只返回一个值 Object:所有类型的祖先
            public static Object ExcuteScalar(string sql)
            {
                SqlConnection conn = new SqlConnection(url);
                try
                {
                    conn.Open();
                    SqlCommand comm = new SqlCommand();
                    comm.Connection = conn;
                    comm.CommandText = sql;
                    Object name = comm.ExecuteScalar();
                    return name;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    return null;
                }
                finally
                {
                    conn.Close();
                }
            }
            public static int ExecuteNonQuery(string sql)
            {
                SqlConnection conn = new SqlConnection(url);
                try
                {
                    conn.Open();
                    SqlCommand comm = new SqlCommand();
                    comm.Connection = conn;
                    comm.CommandText = sql;
                    int sdr = comm.ExecuteNonQuery();
                    return sdr;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    conn.Close();
                    return -1;
                }
            }
        }
    }
